<template>
	<el-main>
		<div style="text-align: center;font-weight: 600;font-size: 16px">
			{{ form.title }}
		</div>
		<div style="margin:20px 0px 20px 0px">
			<div style="display: inline-block">类型：{{ form.voteTypeName }}</div>
			<div style="display: inline-block;float: right;">部门：{{ form.sendDeptName }}</div>
		</div>
		<el-form>
			<el-col :lg="24" style="text-align: center">
				{{ form.beginTime }}~{{ form.endTime }}
			</el-col>
			<div v-html="form.content" style="margin: 15px;font-size: 14px"></div>
			<el-form-item label="相关附件" v-if="form.attachId!='' && form.attachId!=undefined">
				<attach-menu :attachIds="form.attachId" :roleFlag="3" :preview="true"></attach-menu>
			</el-form-item>
			<itemRecord :listData="itemJson"></itemRecord>
		</el-form>
		<el-footer style="text-align: center;">
			<el-button type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</el-footer>
	</el-main>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import itemRecord from '@/views/info/vote/itemRecord'

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu,
		itemRecord
	},
	data() {
		return {
			itemJson: [],
			form: {
				voteId: this.$route.query.voteId,
				title: "",
				voteTypeName: "",
				beginTime: "",
				endTime: "",
				content: "",
				attachId: "",
				itemJson: []
			},
		}
	},
	mounted() {
		this.getMyVoteById();
	},
	methods: {
		async submit() {
			let resJson = [];
			this.itemJson.forEach(item => {
				let json = {
					itemId: item.itemId,
					selected: Object.prototype.toString.call(item.selected) == '[object Number]' ? item.selected : item.selected.join(","),
				}
				resJson.push(json);
			})
			let params = {
				voteId: this.form.voteId,
				result: JSON.stringify(resJson)
			}
			this.isSaveing = true;
			let res = await this.$API.info.vote.insertVoteResult.post(params);
			this.isSaveing = false;
			if (res.code == 200) {
				this.$emit('success')
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			this.visible = false;
		},
		async getMyVoteById() {
			let res = await this.$API.info.vote.getMyVoteById.get({voteId: this.form.voteId});
			if (res.code == 200) {
				this.form.voteId = res.data.voteId
				this.form.title = res.data.title
				this.form.voteTypeName = res.data.voteTypeName
				this.form.sendDeptName = res.data.sendDeptName;
				this.form.beginTime = res.data.beginTime
				this.form.endTime = res.data.endTime
				this.form.content = res.data.content
				this.form.attachId = res.data.attachId
				if (res.data.itemJson != '') {
					let arr = JSON.parse(res.data.itemJson);
					for (let i = 0; i < arr.length; i++) {
						this.itemJson.push(arr[i]);
					}
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		}
	}
}
</script>

<style>
</style>
